/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package easybooking.clients.gui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

/**
 *
 * @author tim
 */
public class EBInterfaceConfigDialog extends JDialog {

    private JLabel lblServerHost;
    private JTextField txfServerHost;
    private JButton btnOk, btnCancel;
    private JPanel buttonPanel;
    private EBInterface ebi;
    private JPanel dialogPanel;

    public EBInterfaceConfigDialog(EBInterface ebif) {
        ebi = ebif;
        //tableData = ebi.ebic.getAllCustomers();
        //generateComponents();

    }
    String getHostname() {
       return txfServerHost.getText();
    }

    public void createAndShowDialog() {
        generateComponents();
        this.setResizable(true);
        this.setTitle("Config Easybooking Client");
        this.setModal(true); // deaktivates input to other dialogs
        this.setLocationRelativeTo(null);
        this.pack();
        this.setVisible(true);
        this.toFront();
    }

    private void generateComponents() {

        dialogPanel = new JPanel();

        // Set LAF to System Standard LAF
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception exc) {
            System.err.println("Error loading L&F: " + exc);
        }

        dialogPanel.setLayout(new GridBagLayout());
        GridBagLayout gbl = new GridBagLayout();

        // Places the GUI elements in the layout using addComponent                                             x, y, w, h, wx, wy
        lblServerHost = new JLabel("EasyBooking-Server:");
        addComponent(dialogPanel, gbl, lblServerHost, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 0, 0, 1, 1, 0, 0);

        txfServerHost = new JTextField(20);
        txfServerHost.setText("localhost");
        addComponent(dialogPanel, gbl, txfServerHost, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 1, 0, 1, 1, 0, 0);

        addComponent(dialogPanel, gbl, this.createButtonPanel(), GridBagConstraints.NONE, GridBagConstraints.EAST, 1, 1, 1, 1, 0, 0);

        this.setContentPane(dialogPanel);

    }

    // Creates the button panel
    private JPanel createButtonPanel() {
        buttonPanel = new JPanel();
        btnOk = new JButton("OK");
        btnCancel = new JButton("Cancel");

        // Sets the buttons the same size
        btnOk.setPreferredSize(btnCancel.getPreferredSize());

        // Registrate action listeners
        btnCancel.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });


        // Adds the buttons to the panel
        buttonPanel.add(btnOk);

        //
        btnOk.setActionCommand("connectToServer");
        btnOk.addActionListener(ebi);

        buttonPanel.add(btnCancel);
        return buttonPanel;
    }

    // Adds the elements to the layout
    static void addComponent(Container cont, GridBagLayout gbl, Component c, int fill, int anchor, int x, int y, int width, int hight, double weightx, double weighty) {
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = fill;
        gbc.anchor = anchor;
        gbc.gridx = x;
        gbc.gridy = y;
        gbc.gridwidth = width;
        gbc.gridheight = hight;
        gbc.weightx = weightx;
        gbc.weighty = weighty;
        gbc.insets = new Insets(10, 10, 10, 10);
        //gbl.setConstraints(c, gbc);
        cont.add(c, gbc);

    }

    
}
